<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="65%">
    <ContentWrap>
      <!-- 搜索工作栏 -->
      <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="100px"
      >
        <el-form-item label="客户名称" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入客户名称"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="客户编号" prop="code">
          <el-input
            v-model="queryParams.code"
            placeholder="请输入客户编号"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="管理单元" prop="manageUnit">
          <el-input
            v-model="queryParams.manageUnit"
            placeholder="请输入管理单元"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="所属区域" prop="area">
          <el-cascader
            v-model="queryParams.area"
            :options="areaList"
            :props="defaultProps"
            class="!w-240px"
            clearable
            filterable
            placeholder="请选择所属区域"
          />
        </el-form-item>
        <el-form-item label="联系人姓名" prop="contactName">
          <el-input
            v-model="queryParams.contactName"
            placeholder="请输入联系人姓名"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="联系电话" prop="contactNumber">
          <el-input
            v-model="queryParams.contactNumber"
            placeholder="请输入联系电话"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item>
          <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
          <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>

    <!-- 列表 -->
    <ContentWrap>
      <el-table
        row-key="id"
        v-loading="loading"
        :data="list"
        :stripe="true"
        :show-overflow-tooltip="true"
        @selection-change="handleRowCheckboxChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column label="客户名称" align="center" prop="name" />
        <el-table-column label="客户编号" align="center" prop="code" />
        <el-table-column label="管理单元" align="center" prop="manageUnit" />
        <el-table-column label="所属区域" align="center" prop="area" />
        <el-table-column label="联系人姓名" align="center" prop="contactName" />
        <el-table-column label="联系电话" align="center" prop="contactNumber" />
        <el-table-column label="服务状态" align="center" prop="serviceStatus" />
      </el-table>
      <!-- 分页 -->
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>

    <template #footer>
      <el-button @click="submit" type="primary" :disabled="loading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>

<script setup lang="ts">
import { isEmpty } from '@/utils/is'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { CustomerArchiveApi, CustomerArchive } from '@/api/biz/customer'
import CustomerArchiveForm from './CustomerArchiveForm.vue'
import { DICT_TYPE, getIntDictOptions, getStrDictOptions } from '@/utils/dict'

import { defaultProps } from '@/utils/tree'
import * as AreaApi from '@/api/system/area'

const emit = defineEmits(['success'])

/** 客户档案 列表 */
defineOptions({ name: 'CustomerArchive' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<CustomerArchive[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: undefined,
  code: undefined,
  preName: undefined,
  manageUnit: undefined,
  area: undefined,
  position: undefined,
  address: undefined,
  industry: undefined,
  contactName: undefined,
  contactNumber: undefined,
  contactDuty: undefined,
  contactAge: undefined,
  introduce: undefined,
  businessSource: undefined,
  companyScale: undefined,
  customerRequire: undefined,
  powerScene: undefined,
  signDifficulty: undefined,
  receiveDifficulty: undefined,
  directorName: undefined,
  directorNumber: undefined,
  directorDuty: undefined,
  directorAge: undefined,
  powerIndustryKnown: undefined,
  powerKnowledge: undefined,
  specialRemark: undefined,
  createTime: [],
  ownerUserId: undefined
})
const queryFormRef = ref() // 搜索的表单
const areaList = ref([]) // 地区列表
const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题

/** 打开弹窗 */
const open = async (id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = '拓展客户'
  handleQuery()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await CustomerArchiveApi.getCustomerArchivePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 查询列表 */
const getAreaList = async () => {
  // 获得地区列表
  areaList.value = await AreaApi.getAreaTree()
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

const customerData = ref({})
const handleRowCheckboxChange = (records: CustomerArchive[]) => {
  customerData.value = records[0]
}

const submit = () => {
  dialogVisible.value = false
  emit('success', customerData.value)
}

/** 初始化 **/
onMounted(() => {
  getAreaList()
  getList()
})
</script>
